import pgzrun
import random
WIDTH=800
HEIGHT=1000
BACK_COLOR=(0,144,103)
board=[[0,0,0,0,0,0,0,0],
       [0,0,0,0,0,0,0,0],
       [0,0,0,3,0,0,0,0],
       [0,0,3,1,2,0,0,0],
       [0,0,0,2,1,3,0,0],
       [0,0,0,0,3,0,0,0],
       [0,0,0,0,0,0,0,0],
       [0,0,0,0,0,0,0,0]]
w=8
h=8
imgs=100
side=1
end=0
spstr=""
again=Actor("again",center=(400,963))

def AI(legal_l):
    randnum = random.randint(0,len(legal_l) - 1)
    return legal_l[randnum][0],legal_l[randnum][1]

def init():
    global board,side,end,spstr
    board=[[0,0,0,0,0,0,0,0],
       [0,0,0,0,0,0,0,0],
       [0,0,0,3,0,0,0,0],
       [0,0,3,1,2,0,0,0],
       [0,0,0,2,1,3,0,0],
       [0,0,0,0,3,0,0,0],
       [0,0,0,0,0,0,0,0],
       [0,0,0,0,0,0,0,0]]
    side=1
    end=0
    spstr=""

def check_list(sx,sy,dx,dy):
    n=[]
    while(0<=sx<w and 0<=sy<h):
        if(board[sx][sy]==0 or board[sx][sy]==3):
            return []
        if(board[sx][sy]==side+1):
            return n
        n.append((sx,sy))
        sx+=dx
        sy+=dy
    return []


def check_down(pos):
    x,y=pos[0],pos[1]
    if(board[x][y]!=3 and board[x][y]!=0):
        return []
    del_list=[]
    del_list+=check_list(x+1,y,1,0) #->
    del_list+=check_list(x-1,y,-1,0) #<-
    del_list+=check_list(x,y+1,0,1) # down
    del_list+=check_list(x,y-1,0,-1) #up
    del_list+=check_list(x-1,y-1,-1,-1) #\up
    del_list+=check_list(x+1,y+1,1,1) #\down
    del_list+=check_list(x+1,y-1,1,-1) #/up
    del_list+=check_list(x-1,y+1,-1,1) #/down
    return del_list

def check_point():
    l = []
    for i in range(0,8):
        for j in range(0,8):
            if check_down((i,j)) != [] and board[i][j] == 3:
                l.append((i,j))
    
    return l[:]


def check_win():
    global spstr
    bl=0
    wh=0
    for i in range(w):
        for j in range(h):
            if(board[i][j]==1):
                wh+=1
            elif(board[i][j]==2):
                bl+=1
    if(bl>wh):
        spstr="black win"
    elif(wh>bl):
        spstr="white win"
    else:
        spstr="draw"

def check_may():
    global side,board,end
    n=0
    for i in range(w):
        for j in range(h):
            m=len(check_down((i,j)))
            if(m!=0):
                board[i][j]=3
                n+=1
            elif(board[i][j]==3):
                board[i][j]=0
    if(n==0):
        side=not side
        end+=1
        if(end==2):
            check_win()
            return
        check_may()
    else:
        end=0
        
def on_mouse_down(pos):
    global side,board
    x,y=pos[0]//imgs,pos[1]//imgs
    if(y>=h or x>=w or board[x][y]!=3):
        if(350<pos[0]<450 and 938<pos[1]<988):
            init()
        return
    l=check_down((x,y))
    if(len(l)!=0):
        board[x][y]=side+1
        for i in l:
            board[i[0]][i[1]]=side+1
        side = not side
    check_may()
    x,y = AI(check_point())
    l=check_down((x,y))
    if(len(l)!=0):
        board[x][y]=side+1
        for i in l:
            board[i[0]][i[1]]=side+1
        side = not side
    check_may()
    return
    

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    bl=0
    wh=0
    for i in range(w):
        for j in range(h):
            n=0
            if(board[i][j]==0):
                n=Actor('none', pos=(i*imgs+imgs//2, j*imgs+imgs//2))
            elif(board[i][j]==1):
                n=Actor('white',pos=(i*imgs+imgs//2, j*imgs+imgs//2))
                wh+=1
            elif(board[i][j]==3):
                n=Actor('may',pos=(i*imgs+imgs//2, j*imgs+imgs//2))
            else:
                n=Actor('black',pos=(i*imgs+imgs//2, j*imgs+imgs//2))
                bl+=1
            n.draw()
    screen.blit("background",(0,800))
    if(side==0):
        screen.draw.text("white's turn", center=(400, 827),fontsize=37)
    elif(spstr!=''):
        screen.draw.text(spstr, center=(400, 827),fontsize=37,color="red")
    else:
        screen.draw.text("black's turn", center=(400, 827),fontsize=37,color="black")
    screen.draw.text("white "+str(wh), midleft=(45, 900),fontsize=120)
    screen.draw.text(str(bl)+" black", midright=(755, 900),fontsize=120,color="black")
    again.draw()

pgzrun.go()
